package cn.jsc.pet.service.impl;

import cn.jsc.basic.service.impl.BaseServiceImpl;
import cn.jsc.basic.util.*;
import cn.jsc.order.domain.PetAcquisitionOrder;
import cn.jsc.order.mapper.PetAcquisitionOrderMapper;
import cn.jsc.org.domain.Employee;
import cn.jsc.org.domain.Shop;
import cn.jsc.org.mapper.EmployeeMapper;
import cn.jsc.org.mapper.ShopMapper;
import cn.jsc.pet.domain.Pet;
import cn.jsc.pet.domain.SearchMasterMsg;
import cn.jsc.pet.mapper.SearchMasterMsgMapper;
import cn.jsc.pet.query.SearchMasterMsgQuery;
import cn.jsc.pet.service.IPetService;
import cn.jsc.pet.service.ISearchMasterMsgService;
import cn.jsc.user.domain.LoginInfo;
import cn.jsc.user.domain.User;
import cn.jsc.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public List<SearchMasterMsg> getAll() {
        return searchMasterMsgMapper.loadAll();
    }

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        //1.给寻主消息对象设置店铺ID
        //用户发布寻主消息的地址
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //获取离用户最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        //设置shop_id
        searchMasterMsg.setShop_id(nearestShop.getId());
        //2.给寻主消息设置最近的用户ID  user_id
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        //设置user_id
        searchMasterMsg.setUser_id(user.getId());
        //3.将寻主消息添加到数据库
        searchMasterMsgMapper.save(searchMasterMsg);
        //4.给店铺发送一条消息，你有新的订单
        //获取店铺管理员的手机号码
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        //SmsUtil.send(employee.getPhone(),"你有新的订单，请到【" + searchMasterMsg.getAddress() + "】去接收宠物" + searchMasterMsg.getName());
        System.out.println("你有新的订单，请到【" + searchMasterMsg.getAddress() + "】去接收宠物" + searchMasterMsg.getName());
    }

    /**
     * 管理员查询待处理消息
     * @param query
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, HttpServletRequest request) {
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfo.getId());
        if(employee.getShop_id() != null){
            //该用户是店铺管理员
            query.setShop_id(employee.getShop_id());
        }
        //查询待处理消息
        query.setState(0);
        //调用父类的分页查询方法
        return super.queryData(query);//state=0，shop_id=null or notnull ,user_id=null
    }
    /**
     * 管理员查询已处理消息
     * @param query
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, HttpServletRequest request) {
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfo.getId());
        if(employee.getShop_id() != null){
            //该用户是店铺管理员
            query.setShop_id(employee.getShop_id());
        }
        //查询待处理消息
        query.setState(1);
        //调用父类的分页查询方法
        return super.queryData(query);//state=1，shop_id=null or notnull ,user_id=null
    }
    /**
     * 获取用户的寻主消息
     * @param query
     * @param request
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> userMsg(SearchMasterMsgQuery query, HttpServletRequest request) {
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        //设置用户的user_id
        query.setUser_id(user.getId());
        //调用父类的分页查询方法
        return super.queryData(query);//state=null，shop_id=null,user_id=有值
    }

    /**
     * 处理宠物寻主信息
     * @param pet
     */
    @Override
    public void handle(Pet pet) {
        //1.待处理变成以处理消息 - 修改state=1通过 待处理消息的id
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        //2.添加宠物相关的信息
        petService.add(pet);//在PetServiceImpl重写add方法实现
        //2.1.添加宠物信息
        //2.2.添加宠物明细

        //3.如果需要钱 - 创建订单进行支付 - 收购订单
        if(Double.parseDouble(pet.getCostprice().toString())>0){//成本价>0 宠物要钱 要从用户那付钱收购
            SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
            Employee employee = employeeMapper.loadByShopId(pet.getShop_id());
            PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, searchMasterMsg, employee.getId());
            petAcquisitionOrderMapper.save(petAcquisitionOrder);
        }
        //4.管理员通过垫付的方式付钱给用户 - 手动把状态改成1
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg masterMsg,Long EmpId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        //摘要
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        //订单唯一标识
        String orderSn = CodeGenerateUtils.generateOrderSn(masterMsg.getUser_id());
        order.setOrderSn(orderSn);
        //状态
        order.setState(0);//待支付
        //收购价-成本价
        order.setPrice(pet.getCostprice());
        //收购地址直接写死：这是收购订单，收购一次之后，这个宠物就没有了，没有必要单独去保存这个订单地址
        order.setAddress(masterMsg.getAddress());
        //支付方式
        order.setPaytype(pet.getPayType());
        //支付唯一标识
        String paySn = CodeGenerateUtils.generateUnionPaySn();
        order.setPaySn(paySn);
        //宠物id
        order.setPet_id(pet.getId());
        //店铺id
        order.setShop_id(pet.getShop_id());
        //店铺员工id
        order.setEmp_id(EmpId);
        return order;
        //lastConfirmTime - 店铺给用户钱，钱给了之后完成了
        //user_id  -  masterMsg里面有
    }
}
